package com.imoblife.brainwavestus.obpurchaseSystem;


import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.imoblife.brainwavestus.model.SpecialsItemInfo;
import com.imoblife.brainwavestus.util.DebugServerPushDataUtil;
import com.imoblife.brainwavestus.util.DebugUtil;
import com.imoblife.brainwavestus.util.ProductUtil;

import android.content.Context;

public class PriceManager {


	private static HashMap<String, SpecialsItemInfo> specialsIProductsHashMap;

	public static void getSpecialsItemInfo(Context context) throws IOException,
			JSONException {

		InputStream inputStream = null;
		if(DebugServerPushDataUtil.isOpenProductJsonDebug){
			try {
				inputStream = context.openFileInput("products.json");
			} catch (Exception e) {
			}
		}
		
		if (inputStream == null) {
			inputStream = context.getAssets().open("products.json");
		}
		

		specialsIProductsHashMap = new HashMap<String, SpecialsItemInfo>();
		byte[] data = new byte[inputStream.available()];

		inputStream.read(data);

		String string = new String(data);
		inputStream.close();

		JSONObject jsonObject = new JSONObject(string);

		JSONArray jsonArray = jsonObject.getJSONArray("Specials");

		String[] products = new String[jsonArray.length()];

		for (int i = 0; i < products.length; i++) {

			SpecialsItemInfo specialsItemInfo = new SpecialsItemInfo();
			JSONObject json = jsonArray.getJSONObject(i);
			specialsItemInfo.productId = json.getString("productId");
			specialsItemInfo.parentId = json.getString("parentId");
			specialsItemInfo.discount = (float) json.getDouble("discount");
			specialsItemInfo.oldPrice = (float) json.getDouble("oldPrice");
			specialsItemInfo.newPrice = (float) json.getDouble("newPrice");

			specialsIProductsHashMap.put(specialsItemInfo.productId,
					specialsItemInfo);

		}

	}
	
	public static boolean getTaskName(Context context,String taskName){
		boolean flag = false;
		if (specialsIProductsHashMap == null) {

			try {
				getSpecialsItemInfo(context);
			} catch (IOException e) {
				
				e.printStackTrace();
			} catch (JSONException e) {
				
				e.printStackTrace();
			}

		}
		SpecialsItemInfo specialsItemInfo = specialsIProductsHashMap
				.get(taskName);
		if(specialsItemInfo!=null){
			flag = true;
		}
		return flag;
	}

	public static String getProductPrice(Context context, String productId,
			String odlePrice) {

		if (specialsIProductsHashMap == null) {

			try {
				getSpecialsItemInfo(context);
			} catch (IOException e) {
				
				e.printStackTrace();
			} catch (JSONException e) {
				
				e.printStackTrace();
			}

		}
		SpecialsItemInfo specialsItemInfo = specialsIProductsHashMap
				.get(productId);
		if (specialsItemInfo == null) {

			return odlePrice;

		}

		return "$"+specialsItemInfo.newPrice;
	}

	public static String getProductOdlePrice(Context context, String productId) {

		if (specialsIProductsHashMap == null) {

			try {
				getSpecialsItemInfo(context);
			} catch (IOException e) {
				
				e.printStackTrace();
			} catch (JSONException e) {
				
				e.printStackTrace();
			}

		}

		SpecialsItemInfo specialsItemInfo = specialsIProductsHashMap
				.get(productId);

		if (specialsItemInfo == null) {
			return null;
		}

		return "$" + specialsItemInfo.oldPrice;
	}

	public static String getProductPrice(Context context, String productId) {

		if (specialsIProductsHashMap == null) {

			try {
				getSpecialsItemInfo(context);
			} catch (IOException e) {
				
				e.printStackTrace();
			} catch (JSONException e) {
				
				e.printStackTrace();
			}

		}
		SpecialsItemInfo specialsItemInfo = specialsIProductsHashMap
				.get(productId);
		if (specialsItemInfo == null) {

			return null;

		}

		return format(specialsItemInfo.oldPrice * specialsItemInfo.discount + 0.5);

	}

	private static String format(double price) {

		return "$" + (int) price + "." + ((int) (price * 100)) % 100;

	}

	public static int getProductSpecials(Context context, String productId) {

		if (specialsIProductsHashMap == null) {

			try {
				getSpecialsItemInfo(context);
			} catch (IOException e) {
				
				e.printStackTrace();
			} catch (JSONException e) {
				
				e.printStackTrace();
			}

		}
		SpecialsItemInfo specialsItemInfo = specialsIProductsHashMap
				.get(productId);
		if (specialsItemInfo == null) {

			return -1;

		}
		
		DebugUtil.debug("", "discount="+(int) (10 - specialsItemInfo.discount * 10 - 1));
		
		return (int) ( specialsItemInfo.discount * 10 -1);
	}

	public static String getTUSGold(String price) {
		String tusPrice = new String(price);
		price = price.replace("$", "");
		price = price.replace(" ", "");

		float tus = 0;

		try {
			tus = Float.parseFloat(price);
		} catch (Exception e) {
			return tusPrice;
		}

		tus *= 10;
		tus += 0.5;

		String tuspoint = "" + ((int) tus);

		return tuspoint;

	}
}
